<template>
    <div class="quality-rule">
        <a-button  icon="plus" @click="handleAdd">新增质量规则</a-button>
        <a-table
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="ipagination"
            :loading="loading"
            class="j-table-force-nowrap"
            @change="handleTableChange">
            <span slot="ruleType" slot-scope="text">
                {{text==1?'私有':text==2?'公有':''}}
            </span>
            <span slot="enableFlag" slot-scope="text">
                <a-tag :color="getStatusColor(text)">{{text?'启用':'停用'}}</a-tag>
            </span>
            <span slot="action" slot-scope="text, record">
              <a @click="enableClick(record)">{{record.enableFlag?'停用':'启用'}}</a>
              <a-divider type="vertical" />
              <a @click="editRule(record.id)">编辑</a>
              <a-divider type="vertical"/>
              <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                <a>删除</a>
              </a-popconfirm>
            </span>
        </a-table>
        <add-rule-modal ref="modalForm" @modalFormOk="modalFormOk"></add-rule-modal>
    </div>
</template>
<script>
import { deleteQualityRule, putEnable } from '@/api/api'
import AddRuleModal from './AddRuleModal'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
const columns = [
  {
    title: '质量规则名称',
    dataIndex: 'ruleName',
  },
  {
    title: '质量规则类别',
    dataIndex: 'ruleType',
    scopedSlots: {customRender: 'ruleType'}
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime'
  },
  {
    title: '是否启用',
    dataIndex: 'enableFlag',
    scopedSlots: {customRender: 'enableFlag'}
  },
  {
    title: '操作',
    scopedSlots: { customRender: 'action' },
    align: 'left',
    width: '100px'
  },
]
export default {
    name:'QualityRule',
    mixins:[JeecgListMixin],
    components: {
        AddRuleModal
    },
    data(){
        return {
            columns,
            url: {
                list: "/quality/rule/list"
                // deleteBatch: "/sys/tenant/deleteBatch"
            },
        }
    },
    watch:{
        $route: {
            handler: function(route) {
                this.queryParam.modelId = Number(this.$route.query.modelId)
            },
            immediate: true,
            
        }
    },
    mounted() {
        console.log(this.ipagination,'ipagination');
    },
    methods: {
        enableClick(row){
            let params = { 
                id:row.id,
                enable:row.enableFlag?false:true
            }
            putEnable(params).then(res=>{
                if (res.success) {
                    this.$message.success(res.message);
                    this.loadData();
                } else {
                    this.$message.warning(res.message);
                }
            })
        },
        editRule(id){
            this.$refs.modalForm.title = "编辑规则";
            this.$refs.modalForm.formData.id=id
            this.$refs.modalForm.formData.modelId = this.queryParam.modelId;
            this.$refs.modalForm.edit();                                                    
        },
        getStatusColor(code){
            return code?'green':'gray'
        },
        handleAdd() {
            this.$refs.modalForm.title = "新增规则";
            this.$refs.modalForm.formData.modelId = this.queryParam.modelId;
            this.$refs.modalForm.add();
        },
        handleDelete(id){
            deleteQualityRule({id}).then(res=>{
                if (res.success) {
                    //重新计算分页问题
                    this.reCalculatePage(1)
                    this.$message.success(res.message);
                    this.loadData();
                } else {
                    this.$message.warning(res.message);
                }
            })
        }
    }
}
</script>
<style lang="less" scoped>
.quality-rule{
    .j-table-force-nowrap{
        margin-top:15px;
    }
}
</style>